// pages/goods_detail/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        buyPop:false,
        id:0,
        goods_detail:null,
        buy_num:1,
        select_num:1
    },
    buyPopToggle(){
        this.setData({
            buyPop:!this.data.buyPop
        })
    },
    cut_num() {
        console.log(11)
        if (this.data.buy_num == 1) {
          return;
        } else {
          this.data.buy_num--;
          this.setData({
            buy_num: this.data.buy_num
          })
        }
      },
       //增加数量
  add_num() {
    console.log(112)
    var that =this;
    request.requestLoading(
        "/api/goods/checkStock",
        {
            goods_id:that.data.id,     
            num:parseInt(that.data.buy_num)+1,
             },
        'POST',
      ).then((res) => {
          if(res.code==1){
         
            that.setData({
                buy_num: that.data.buy_num+1
            }) 
          }else{
              request.toast(res.msg)
          }
      
      
       
       
  
      }).catch((err) => {
        console.error(err);
      });
   
       
     
       
       
  
   

  },
  buy_num(e) {
    console.log(1123)
    if (!this.data.buy_num) {
      wx.showModal({
        title: "提示",
        content: '请填写正确的数量'
      })
      this.setData({
        buy_num: 1
      })
      return false;
    }
    if (this.data.buy_num == 0) {
      wx.showModal({
        title: "提示",
        content: '请填写正确的数量'
      })
      this.setData({
        buy_num: 1
      })
      return false;
    }
  
    var that =this;
    request.requestLoading(
        "/api/goods/checkStock",
        {
            goods_id:that.data.id,     
            num:parseInt(that.data.buy_num)+1,
             },
        'POST',
      ).then((res) => {
          if(res.code!=1){
            request.toast(res.msg);
            that.setData({
                buy_num:res.data
            })
        }
      }).catch((err) => {
        console.error(err);
      });
    
  },
  input(e) {
  
    this.setData({
        select_num:this.data.buy_num,
        buy_num:e.detail.value
       
    })


  },
    checkLogin(){
        if(wx.getStorageSync('token')){
            
            return true
        }else{
            wx.showModal({
              title: '提示',
              content: '您还未登录，去登录',
              complete: (res) => {
                if (res.cancel) {
                    return false;
                }
            
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/login/index',
                  })
                }
              }
            })
            return false;
        }
    },
    buyNow(){
        if(!this.checkLogin()){
            return false;
        }  
        var that = this;
        let confirmcartList = [];
        if(that.data.buy_num<=0){
           wx.showToast({
             title: '购物数量需要大于0',
             icon:'none'
           })
           return false;
        }
        request.requestLoading(
            "/api/goods/checkStock",
            {
                goods_id:that.data.id,     
                num:that.data.buy_num,
                 },
            'POST',
          ).then((res) => {
              if(res.code==1){
                wx.navigateTo({
                    url: '/pages/make_order/index?goodsId=' +  that.data.id + '&&goodnum=' + that.data.buy_num + '&&key=0',
                  })
              }else{
                  request.toast(res.msg)
              }
          
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
     
      
         
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      if(options.id){
          this.setData({
              id:options.id
          })
      }
      this.getDetail()
    },
    getDetail()
    {
        var that = this;
        var json = {};
       json.id = that.data.id;
        request.requestLoading(
            "/api/goods/goodsDetail",
            json,
            'GET',
          ).then((res) => {
              console.log(res)
              
            if(res.data){
              that.setData({
                goods_detail:res.data
              })
              if(that.data.goods_num==0){
                  that.setData({
                      buy_num:0
                  })
              }
             
           
            }
            that.setData({
                is_show:true
              })
      
          }).catch((err) => {
            console.error(err);
          });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getDetail()
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})